package com.inspinia.base.util;

import com.inspinia.base.common.model.BaseConstants;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.joda.time.LocalDateTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by likun on 2016/4/26 0026.
 */
public class DateUtil {

    public static final long MS_ONE_DAY = 1000 * 60 * 60 * 24;

    public static final long MS_TO_HOUR_UINT = 1000 * 60 * 60;

    public static final FastDateFormat format = FastDateFormat.getInstance(BaseConstants.DATE_TIME_PATTERN, Locale.ENGLISH);
    public static final FastDateFormat simpleFormat = FastDateFormat.getInstance(BaseConstants.DATE_PATTERN, Locale.ENGLISH);
    public static final FastDateFormat simpleFormatD = FastDateFormat.getInstance(BaseConstants.DATE_PATTERN_D, Locale.ENGLISH);

    /**
     * 根据传入日期获取当前年份
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getYear(Date date) throws ParseException {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.YEAR) + "";
    }

    /**
     * 根据传入日期获取当前年份
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static int getYear(String date) throws ParseException {
        Date start = simpleFormat.parse(date);
        Calendar c = Calendar.getInstance();
        c.setTime(start);
        return c.get(Calendar.YEAR);
    }

    /**
     * 计算两个日期相差多少年
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getDifferenceYear(String startDate, String endDate) throws ParseException {
        Date start = simpleFormat.parse(startDate);
        Date end = simpleFormat.parse(endDate);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(start);
        c2.setTime(end);
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        return Math.abs(year1 - year2);
    }

    /**
     * 计算两个日期相差几天
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 相差几天
     */
    public static long difftime(String startDate, String endDate) throws ParseException {
        /* 先转成毫秒并求差 */
        long m = simpleFormat.parse(endDate).getTime() - simpleFormat.parse(startDate).getTime();
        /* 根据你的需求进行单位转换 */
        return m / (1000 * 60 * 60 * 24);
    }

    /**
     * 字符串转日历
     *
     * @param endDate 要转换的日期Constants.DATE_PATTERN
     * @return
     */
    public static Calendar String2Calendar(String endDate) throws ParseException {
        Date date = simpleFormat.parse(endDate);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * 起始年月yyyy-MM与终止月yyyy-MM之间跨度的月数。
     *
     * @param startDate 格式为yyyy-MM
     * @param endDate   格式为yyyy-MM
     * @return 整数。
     */
    public static int getMonth(String startDate, String endDate) {
        int intBeginYear = Integer.parseInt(startDate.substring(0, 4));
        int intBeginMonth = Integer.parseInt(startDate.substring(startDate
                .indexOf("-") + 1));
        int intEndYear = Integer.parseInt(endDate.substring(0, 4));
        int intEndMonth = Integer.parseInt(endDate.substring(endDate
                .indexOf("-") + 1));

        return ((intEndYear - intBeginYear) * 12)
                + (intEndMonth - intBeginMonth) + 1;
    }

    /**
     * 获取当前年月(yyyy-MM)
     *
     * @return
     */
    public static String getNowMonth() {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        int year = cal.get(Calendar.YEAR);
        return year + "-" + (month < 10 ? "0" + month : month);
    }

    /**
     * 获取某一月
     *
     * @param i 偏移量,小于0向前偏移,大于0向后偏移
     * @return
     */
    public static String getAnyMonth(int i) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, i);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String time = format.format(c.getTime());
        return time;
    }

    /**
     * 获取某月一号
     *
     * @param i 偏移量,小于0向前偏移,大于0向后偏移
     * @return
     */
    public static Long getMonthFirstDay(int i) {
        Long firstDay = null;
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, i);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String time = format.format(c.getTime()) + "-01";
        try {
            firstDay = new SimpleDateFormat(BaseConstants.DATE_PATTERN).parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return firstDay;
    }

    /**
     * 获取某月最后一天
     *
     * @param i 偏移量,小于0向前偏移,大于0向后偏移
     * @return
     */
    public static Long getMonthLastDay(int i) {
        Long lastdat = null;
        //得到目标月下月的一号
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, i + 1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String time = format.format(c.getTime()) + "-01";
        try {
            Long firstDay = new SimpleDateFormat(BaseConstants.DATE_PATTERN).parse(time).getTime();
            //得到前一天
            lastdat = firstDay - MS_ONE_DAY;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //得到前一天
        return lastdat;
    }

    /**
     * 判断目标日期是否在指定日期之前(包含)
     *
     * @param targetDate 目标日期
     * @param date       指定日期
     * @return
     */
    public static boolean isBefor(String targetDate, String date) {

        //如果两个日期相等则直接返回true
        if (targetDate.equals(date)) {
            return true;
        }

        try {
            SimpleDateFormat format = new SimpleDateFormat(BaseConstants.DATE_PATTERN_D);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(format.parse(date));
            Calendar targetCalendar = Calendar.getInstance();
            targetCalendar.setTime(format.parse(targetDate));

            return targetCalendar.before(calendar);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 计算两个日期之间有多少周
     *
     * @param startdate
     * @param enddate
     * @return
     */
//    public static long getweeks(String startdate, String enddate) {
//        long temp = 0;
//        try {
//            SimpleDateFormat sdf = new SimpleDateFormat(BaseConstants.DATE_PATTERN);
//            Date dateStart = sdf.parse(startdate);
//            Date dateEnd = sdf.parse(enddate);
//            temp = (long) ((dateEnd.getTime() - dateStart.getTime()) / 86400000 / 7);
//        } catch (Exception ee) {
//            ee.printStackTrace();
//        }
//        return temp;
//    }
    public static long getweeks(String startdate, String enddate) throws ParseException {
        long count = difftime(startdate, enddate);
        long week = count / 7;
        if (count % 7 != 0) {
            week++;
        }
        return week;
    }

    /**
     * 把yyyy-mm-dd格式的字符串转换成周yyyy-ww格式
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String string2Week(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(BaseConstants.DATE_PATTERN);
        Date d = sdf.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        String year = calendar.get(Calendar.YEAR) + "";
        int day = calendar.get(Calendar.DAY_OF_YEAR);
        int week = day % 7 == 0 ? day / 7 : day / 7 + 1;
        String weekStr = week < 10 ? "0" + week : week + "";
//        SimpleDateFormat sdfWeek = new SimpleDateFormat("yyyy-ww");
        return year + "-" + weekStr;
    }

    /**
     * 字符串转日历  yyyy-MM
     *
     * @param time
     * @return
     */
    public static Calendar String2CalendarMonth(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * 将当前时间格式化为"yyyy-mm"格式的字符串
     *
     * @param date
     * @return
     */
    public static String formatDateMonth(Date date) {
        return new SimpleDateFormat("yyyy-MM").format(date);
    }

    /**
     * 将当前时间格式化为"yyyy-mm"格式的字符串
     *
     * @param date
     * @return
     */
    public static String formatDateYear(Date date) {
        return new SimpleDateFormat("yyyy").format(date);
    }

    /**
     * 将当前时间格式化为Constants.DATE_PATTERN格式的字符串
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        return new SimpleDateFormat(BaseConstants.DATE_PATTERN).format(date);
    }

    /**
     * 将时间转换为指定格式
     * FastDateFormat会自动根据时间格式缓存下创建过的转换对象，不会每次都创建新的对象
     * {@link org.apache.commons.lang3.time.FormatCache#getInstance(String, TimeZone, Locale)}
     * @param date 日期时间
     * @param format 日期格式
     * @return
     */
    public static String formatDate(Date date,String format){
        return FastDateFormat.getInstance(format).format(date);
    }

    /**
     * 获取date的日期形式的日期对象
     * 比如参数中的日期是:2016-02-02 09:02:22,那么返回值应该是2016-02-02 00:00:00的日期类型。
     *
     * @param date 日期
     * @return
     */
    public static Date getDayDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        cal.set(year, month, day, 0, 0, 0);
        return cal.getTime();
    }

    /**
     * 计算日期相差天数
     *
     * @param date      日期
     * @param otherDate 另一个日期
     * @return 相差天数
     */
    public static int getIntervalDays(Date date, Date otherDate) {
        double interval = Math.abs((date.getTime() - otherDate.getTime()) / Double.valueOf(BaseConstants.ONE_DAY_MS));
        return (int) Math.ceil(interval);
    }

    /**
     * 根据传入的开始时间和结束时间计算着两个日期横跨了多少周
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 横跨了多少周的集合
     */
    public static HashSet<String> getWeekList(String startDate, String endDate) {
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(BaseConstants.DATE_PATTERN);
        SimpleDateFormat sdfWeek = new SimpleDateFormat("yyyy-ww");
        HashSet<String> weekList = new HashSet();
        try {
            startCalendar.setTime(sdf.parse(startDate));
            endCalendar.setTime(sdf.parse(endDate));
            while (startCalendar.compareTo(endCalendar) > 0) {
                String week = sdfWeek.format(startCalendar.getTime());
                startCalendar.add(Calendar.DATE, 1);
                weekList.add(week);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return weekList;
    }

    /**
     * 把时间设置为23:59:59
     *
     * @param time
     * @return
     */
    public static Date toMaxDate(Date time) {
        if (time == null) {
            return time;
        }
        LocalDateTime ld = new LocalDateTime(time).hourOfDay().setCopy(23).minuteOfHour().setCopy(59).secondOfMinute().setCopy(59).millisOfSecond().setCopy(999);
        return ld.toDate();
    }

    /**
     * 计算当前时间和下个月之间的毫秒数
     *
     * @return
     */
    public static Long getDifEndOfMonthMillsecond() {
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        int actualMaximum = gregorianCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        gregorianCalendar.set(Calendar.DAY_OF_MONTH, actualMaximum);
        gregorianCalendar.set(Calendar.HOUR_OF_DAY, 24);
        gregorianCalendar.set(Calendar.SECOND, 0);
        gregorianCalendar.set(Calendar.MINUTE, 0);
        Date time = gregorianCalendar.getTime();
        long millisecond = time.getTime() - System.currentTimeMillis();
        return millisecond;
    }

    /**
     * 将毫秒时间转成时分秒
     *
     * @param l
     * @return
     */
    public static String formatLongToTimeStr(Long l) {
        String str = "";
        int hour = 0;
        int minute = 0;
        int second = 0;
        second = l.intValue() / 1000;
        if (second > 60) {
            minute = second / 60;
            second = second % 60;
        }
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        String strtime = hour + "小时" + minute + "分钟" + second + "秒";
        return strtime;
    }

    public static String formatDuring(long mss) {
        long days = mss / (1000 * 60 * 60 * 24);
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;
        return days + "日" + hours + "小时" + minutes + "分"
                + seconds + "秒";
    }

    /**
     * 获取某段时间内的所有日期
     *
     * @param start
     * @param end
     * @return
     * @throws ParseException
     */
    public static List<Date> findDates(String start, String end) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(BaseConstants.DATE_PATTERN);
        Date dBegin = sdf.parse(start);
        Date dEnd = sdf.parse(end);
        List lDate = new ArrayList();
        //lDate.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        lDate.add(calBegin.getTime());
        while (dEnd.after(calBegin.getTime())) {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }

    public static List<String> findDates(Date start, Date end) {
        if (start == null || end == null) {
            return null;
        }
        List<String> lDate = new ArrayList();
        //lDate.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(start);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(end);
        // 测试此日期是否在指定日期之后
//        lDate.add(formatDate(calBegin.getTime()));
        while (end.after(calBegin.getTime())) {
            lDate.add(formatDate(calBegin.getTime()));
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
        }
        return lDate;
    }

    /**
     * 判断否个时间是否在两个时间范围内
     *
     * @param time      比较时间
     * @param startTime 时间段的开始时间
     * @param endTime   时间段的结束时间
     * @return
     */
    public static boolean isTimeInScope(Long time, Date startTime, Date endTime) {
        if (null == startTime && null == endTime) {
            return true;
        }
        if (startTime != null && endTime != null) {
            return time.compareTo(startTime.getTime()) >= 0 && time.compareTo(endTime.getTime()) <= 0;
        }
        if (startTime == null && endTime != null) {
            return time.compareTo(endTime.getTime()) <= 0;
        }
        if (endTime == null && startTime != null) {
            return time.compareTo(startTime.getTime()) >= 0;
        }
        return false;
    }

    /**
     * 获取当天之从0点到24点时间
     *
     * @return
     */
    public static Date[] weekTimeAfterToday() throws ParseException {
        Date[] dates = new Date[2];
        String today = simpleFormat.format(new Date());
        String startTodayTime = today + " 00:00:00";
        String endTodayTime = today + " 23:59:59";
        Date todayStime = format.parse(startTodayTime);
        Date todayEtime = format.parse(endTodayTime);
        dates[0] = todayStime;
        dates[1] = todayEtime;
        return dates;
    }

    /**
     * 获取指定日期之从0点到24点时间
     *
     * @return
     */
    public static Date[] weekTimeAfterToday(Date date) throws ParseException {
        Date[] dates = new Date[2];
        String today = simpleFormat.format(date);
        String startTodayTime = today + " 00:00:00";
        String endTodayTime = today + " 23:59:59";
        Date todayStime = format.parse(startTodayTime);
        Date todayEtime = format.parse(endTodayTime);
        dates[0] = todayStime;
        dates[1] = todayEtime;
        return dates;
    }

    /**
     * 获取指定日期的N天前/后的的日期
     *
     * @param offset 偏移量，大于0为N天后，小于0为N天前
     * @return
     */
    public static String getSpecifiedDateOffset(String SpecDate, int offset, SimpleDateFormat format) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(simpleFormat.parse(SpecDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.add(Calendar.DATE, offset);
        Date time = cal.getTime();
        return format.format(time);
    }

    /**
     * 获取指定日期的N天前/后的的日期
     *
     * @param offset 偏移量，大于0为N天后，小于0为N天前
     * @return
     */
    public static String getSpecifiedDateOffset(Date specDate, int offset, SimpleDateFormat format) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(specDate);
        cal.add(Calendar.DATE, offset);
        Date time = cal.getTime();
        return format.format(time);
    }

    /**
     * 获取指定日期的N天前/后的的日期,返回Date类型
     *
     * @param specDate
     * @param offset   偏移量，大于0为N天后，小于0为N天前
     * @return
     */
    public static Date getSpecifiedDateOffset(Date specDate, int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(specDate);
        calendar.add(Calendar.DATE, offset);
        Date date = calendar.getTime();
        return date;
    }

    /**
     * 获取指定日期0点
     *
     * @param specDate
     * @param offset   偏移量，大于0为N天后，小于0为N天前
     * @return
     */
    public static Date getSpecifiedDateZeroOfficeOclock(Date specDate, int offset) {
        String specDateStr = simpleFormat.format(specDate);
        try {
            specDate = format.parse(specDateStr + " 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(specDate);
        calendar.add(Calendar.DATE, offset);
        Date date = calendar.getTime();
        return date;
    }

    /**
     * 获取指定日期波动N天的时间范围
     *
     * @param specDate 指定日期
     * @param offset   波动时间
     * @return
     */
    public static List<Date> getSpecifiedDateRange(Date specDate, int offset) {
        List<Date> dates = new ArrayList<>();

        try {
            //获取前N天0点
            Date startDate = format.parse(simpleFormat.format(getSpecifiedDateOffset(specDate, -offset)) + " 00:00:00");
            dates.add(startDate);
            //获取后N+1天0点
            Date endDate = format.parse(format.format(getSpecifiedDateOffset(specDate, offset + 1)) + " 00:00:00");
            dates.add(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return dates;
    }

    /**
     * 将表名转为yyyy-MM-dd格式的日期
     *
     * @param tableName
     * @return
     */
    public static String tableNameTransToDate(String tableName) {
        String tableDate = tableName.replace("e_cdr_", "");
        try {
            Date tempDate = new SimpleDateFormat(BaseConstants.DATE_PATTERN_D).parse(tableDate);
            return simpleFormat.format(tempDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 分析时差
     *
     * @param dateStr
     * @return
     */
    public static Integer getTimeDiff(String dateStr) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        long remoteTime = format.parse(format.format(new Date(dateStr))).getTime();
        long localTime = format.parse(format.format(new Date())).getTime();
        Long timeDiff = (localTime - remoteTime) / MS_TO_HOUR_UINT;
        return timeDiff.intValue();
    }

    /**
     * 字符串转时间
     *
     * @param dateStr
     * @param rule
     * @return
     * @throws ParseException
     */
    public static Date getDate(String dateStr, String rule) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(rule);
        return format.parse(dateStr);
    }

    /**
     * 获取一周的开始时间和结束时间
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static String[] getOneWeekTime(String startTime, String endTime) {
        String[] times = new String[2];
        if (strsIsNull(startTime, endTime)) {
            endTime = DateFormatUtils.format(new Date(), BaseConstants.DATE_PATTERN) + " 23:59:59";
            Calendar oneWeekTime = Calendar.getInstance();
            oneWeekTime.add(Calendar.WEEK_OF_YEAR, -1);
            startTime = DateFormatUtils.format(oneWeekTime, BaseConstants.DATE_PATTERN) + " 00:00:00";
        }
        times[0] = startTime;
        times[1] = endTime;
        return times;
    }

    /**
     * 判断传入的参数是否为空
     *
     * @param strs
     * @return
     */
    public static boolean strsIsNull(String... strs) {
        for (String s : strs) {
            if (s != null && !"".equals(s) && !"undefined".equals(s)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 获取开始时间和结束之前的数组
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getDayArray(String startTime, String endTime) {
        List<String> list = new ArrayList<>();
        try {
            Date date = format.parse(startTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            String startDate = startTime.substring(0,10);
            String endDate = endTime.substring(0,10);
            list.add(startDate);
            while (!startDate.equals(endDate)) {
                calendar.add(Calendar.DATE, +1);
                startDate = simpleFormat.format(calendar);
                list.add(startDate.substring(0,10));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取当天所在周的开始结束日期
     * @return yyyy-MM-dd
     */
    public static String[] getOneWeek(){
        Calendar cal = Calendar.getInstance();
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);
        String monday = formatDate(cal.getTime(), BaseConstants.DATE_PATTERN);
        cal.add(Calendar.DATE, 6);
        String sunday = formatDate(cal.getTime(),BaseConstants.DATE_PATTERN);
        String[] oneWeek = new String[2];
        oneWeek[0] = monday;
        oneWeek[1] = sunday;
        return oneWeek;
    }

    /**
     * 根据日期获取所在周 周1 和周日的日期
     * @param date yyyy-MM-dd HH:mm:ss
     * @return yyyy-MM-dd
     * @throws ParseException
     */
    public static String[] getFirstAndLastOfWeek(Date date) throws ParseException {
        Calendar monday = Calendar.getInstance();
        String[] times = new String[2];
        monday.setTime(date);
        monday.setFirstDayOfWeek(Calendar.MONDAY);
        monday.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        times[0] = formatDate(monday.getTime(),BaseConstants.DATE_PATTERN);
        monday.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
        times[1] = formatDate(monday.getTime(),BaseConstants.DATE_PATTERN);
        return times;
    }

    /**
     * 根据日期获取所在月 第一天 和最后一天的日期
     * @param date yyyy-MM-dd HH:mm:ss
     * @return
     * @throws ParseException
     */
    public static String[] getFirstAndLastOfMonth(Date date) throws ParseException {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        c.add(Calendar.MONTH, 0);
        //设置为1号,本月第一天
        c.set(Calendar.DAY_OF_MONTH,1);
        String first = formatDate(c.getTime(),BaseConstants.DATE_PATTERN);

        //获取月最后一天
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = formatDate(c.getTime(),BaseConstants.DATE_PATTERN);
        String[] days = new String[2];
        days[0] = first;
        days[1] = last;
        return days;
    }

    /**
     * 取得季度月
     *
     * @param date
     * @return
     */
    public static Date[] getSeasonDate(Date date) {
        Date[] season = new Date[3];

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int nSeason = getSeason(date);
        if (nSeason == 1) {// 第一季度
            c.set(Calendar.MONTH, Calendar.JANUARY);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.FEBRUARY);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.MARCH);
            season[2] = c.getTime();
        } else if (nSeason == 2) {// 第二季度
            c.set(Calendar.MONTH, Calendar.APRIL);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.MAY);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.JUNE);
            season[2] = c.getTime();
        } else if (nSeason == 3) {// 第三季度
            c.set(Calendar.MONTH, Calendar.JULY);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.AUGUST);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.SEPTEMBER);
            season[2] = c.getTime();
        } else if (nSeason == 4) {// 第四季度
            c.set(Calendar.MONTH, Calendar.OCTOBER);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.NOVEMBER);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.DECEMBER);
            season[2] = c.getTime();
        }
        return season;
    }

    /**
     *
     * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
     *
     * @param date
     * @return
     */
    public static int getSeason(Date date) {

        int season = 0;

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(Calendar.MONTH);
        switch (month) {
            case Calendar.JANUARY:
            case Calendar.FEBRUARY:
            case Calendar.MARCH:
                season = 1;
                break;
            case Calendar.APRIL:
            case Calendar.MAY:
            case Calendar.JUNE:
                season = 2;
                break;
            case Calendar.JULY:
            case Calendar.AUGUST:
            case Calendar.SEPTEMBER:
                season = 3;
                break;
            case Calendar.OCTOBER:
            case Calendar.NOVEMBER:
            case Calendar.DECEMBER:
                season = 4;
                break;
            default:
                break;
        }
        return season;
    }

    /**
     * 取得月第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDateOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    /**
     * 取得月最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDateOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getCurrYearFirst(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取某年最后一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getCurrYearLast(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();

        return currYearLast;
    }

    /**
     * 获取某年
     * @param date
     */
    public static int getYearByDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }
}
